home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JDBC / JDODB101 / JDBC-ODB.ZIP / jdbc-odbc / simpleselect.java < prev   
Encoding:
Java Source  |  1996-09-20  |  4.7 KB  |  194 lines

  1. //----------------------------------------------------------------------------
  2. //
  3. // Module:    simpleselect.java
  4. //
  5. // Description:    Test program for ODBC API interface.  This java application
  6. //        will connection to a JDBC driver, issue a select statement
  7. //        and display all result columns and rows
  8. //
  9. // Product:    JDBC to ODBC Bridge
  10. //
  11. // Author:    Karl Moss
  12. //
  13. // Date:    February, 1996
  14. //
  15. // Copyright:    1990-1996 INTERSOLV, Inc.
  16. //        This software contains confidential and proprietary
  17. //        information of INTERSOLV, Inc.
  18. //----------------------------------------------------------------------------
  19.  
  20. import java.net.URL;
  21. import java.sql.*;
  22.  
  23. class simpleselect {
  24.  
  25.     public static void main (String args[]) {
  26.  
  27.         String url   = "jdbc:odbc:my-dsn";
  28.         String query = "SELECT * FROM emp"; 
  29.  
  30.         try {
  31.  
  32.             // Load the jdbc-odbc bridge driver
  33.  
  34.             Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  35.  
  36.             // Attempt to connect to a driver.  Each one
  37.             // of the registered drivers will be loaded until
  38.             // one is found that can process this URL
  39.  
  40.             Connection con = DriverManager.getConnection (
  41.                     url, "my-user", "my-passwd");
  42.  
  43.             // If we were unable to connect, an exception
  44.             // would have been thrown.  So, if we get here,
  45.             // we are successfully connected to the URL
  46.  
  47.             // Check for, and display and warnings generated
  48.             // by the connect.
  49.  
  50.             checkForWarning (con.getWarnings ());
  51.  
  52.             // Get the DatabaseMetaData object and display
  53.             // some information about the connection
  54.  
  55.             DatabaseMetaData dma = con.getMetaData ();
  56.  
  57.             System.out.println("\nConnected to " + dma.getURL());
  58.             System.out.println("Driver       " + 
  59.                 dma.getDriverName());
  60.             System.out.println("Version      " +
  61.                 dma.getDriverVersion());
  62.             System.out.println("");
  63.  
  64.             // Create a Statement object so we can submit
  65.             // SQL statements to the driver
  66.  
  67.             Statement stmt = con.createStatement ();
  68.  
  69.             // Submit a query, creating a ResultSet object
  70.  
  71.             ResultSet rs = stmt.executeQuery (query);
  72.  
  73.             // Display all columns and rows from the result set
  74.  
  75.             dispResultSet (rs);
  76.  
  77.             // Close the result set
  78.  
  79.             rs.close();
  80.  
  81.             // Close the statement
  82.  
  83.             stmt.close();
  84.  
  85.             // Close the connection
  86.  
  87.             con.close();
  88.         }
  89.         catch (SQLException ex) {
  90.  
  91.             // A SQLException was generated.  Catch it and
  92.             // display the error information.  Note that there
  93.             // could be multiple error objects chained
  94.             // together
  95.  
  96.             System.out.println ("\n*** SQLException caught ***\n");
  97.  
  98.             while (ex != null) {
  99.                 System.out.println ("SQLState: " +
  100.                     ex.getSQLState ());
  101.                 System.out.println ("Message:  " +
  102.                     ex.getMessage ());
  103.                 System.out.println ("Vendor:   " +
  104.                     ex.getErrorCode ());
  105.                 ex = ex.getNextException ();
  106.                 System.out.println ("");
  107.             }
  108.         }
  109.         catch (java.lang.Exception ex) {
  110.  
  111.             // Got some other type of exception.  Dump it.
  112.  
  113.             ex.printStackTrace ();
  114.         }
  115.     }
  116.  
  117.     //-------------------------------------------------------------------
  118.     // checkForWarning
  119.     // Checks for and displays warnings.  Returns true if a warning
  120.     // existed
  121.     //-------------------------------------------------------------------
  122.  
  123.     private static boolean checkForWarning (SQLWarning warn)
  124.             throws SQLException
  125.     {
  126.         boolean rc = false;
  127.  
  128.         // If a SQLWarning object was given, display the
  129.         // warning messages.  Note that there could be
  130.         // multiple warnings chained together
  131.  
  132.         if (warn != null) {
  133.             System.out.println ("\n *** Warning ***\n");
  134.             rc = true;
  135.             while (warn != null) {
  136.                 System.out.println ("SQLState: " +
  137.                     warn.getSQLState ());
  138.                 System.out.println ("Message:  " +
  139.                     warn.getMessage ());
  140.                 System.out.println ("Vendor:   " +
  141.                     warn.getErrorCode ());
  142.                 System.out.println ("");
  143.                 warn = warn.getNextWarning ();
  144.             }
  145.         }
  146.         return rc;
  147.     }
  148.  
  149.     //-------------------------------------------------------------------
  150.     // dispResultSet
  151.     // Displays all columns and rows in the given result set
  152.     //-------------------------------------------------------------------
  153.  
  154.     private static void dispResultSet (ResultSet rs)
  155.         throws SQLException
  156.     {
  157.         int i;
  158.  
  159.         // Get the ResultSetMetaData.  This will be used for
  160.         // the column headings
  161.  
  162.         ResultSetMetaData rsmd = rs.getMetaData ();
  163.  
  164.         // Get the number of columns in the result set
  165.  
  166.         int numCols = rsmd.getColumnCount ();
  167.  
  168.         // Display column headings
  169.  
  170.         for (i=1; i<=numCols; i++) {
  171.             if (i > 1) System.out.print(",");
  172.             System.out.print(rsmd.getColumnLabel(i));
  173.         }
  174.         System.out.println("");
  175.         
  176.         // Display data, fetching until end of the result set
  177.  
  178.         while (rs.next ()) {
  179.  
  180.             // Loop through each column, getting the
  181.             // column data and displaying
  182.  
  183.             for (i=1; i<=numCols; i++) {
  184.                 if (i > 1) System.out.print(",");
  185.                 System.out.print(rs.getString(i));
  186.             }
  187.             System.out.println("");
  188.  
  189.             // Fetch the next result set row
  190.  
  191.         }
  192.     }
  193. }
  194.